package com.lemon.smartsafetynew.ui.activity.patrol;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafetynew.entity.common.EnterTypeEnum;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolChildItemDetailHistoryVo;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolChildItemHistoryVo;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolHistoryItemVo;
import com.lemon.smartsafetynew.net.services.SafePatrolServices;
import com.lemon.smartsafetynew.ui.adapter.patrol.PatrolCheckItemHistoryAdapter;
import com.lemon.smartsafetynew.util.constants.KeyConstants;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import kale.adapter.LemonRcvAdapter;

/**
 * Created by Andrew on 2017/8/3.
 */

public class SafePatrolHistoryDetailActivity extends SwipeBackActivity
        implements RecyclerTClickListener<SafePatrolChildItemDetailHistoryVo> {

    @Bind(R.id.safe_patrol_name)
    TextView safePatrolName;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.attachment_recyclerView)
    RecyclerView attachmentRecyclerView;

    private LemonRcvAdapter<SafePatrolChildItemDetailHistoryVo> mAdapter;
    private List<SafePatrolChildItemDetailHistoryVo> mDataList = new ArrayList<>();

    private static SafePatrolHistoryItemVo historyItemVo;

    @Override
    protected void initTitle() {
        this.setBarTitle("巡查历史详情");
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_safe_patrol_history_detail;
    }

    @Override
    protected void getBundleArgs() {

        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {
            historyItemVo = (SafePatrolHistoryItemVo) bundle.getSerializable(KeyConstants.KEY_SAFE_PATROL_CHECK_ITEM_HISTORY_RESULT);
        } else {
            callFinish();
        }
    }

    @Override
    protected void initViews() {
        super.initViews();

        initRecyclerView();

        // ====== Get Net Data ======
        callHistoryChildItem();
    }

    private void initRecyclerView() {

        LinearLayoutManager layoutManager = new LinearLayoutManager(IApplication.context(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setNestedScrollingEnabled(false);
        mAdapter = new PatrolCheckItemHistoryAdapter(mDataList, this);
        recyclerView.setAdapter(mAdapter);

    }

    @Override
    public void onItemClick(SafePatrolChildItemDetailHistoryVo data, int position) {

        Bundle bundle = new Bundle();
        bundle.putSerializable(KeyConstants.KEY_SAFE_PATROL_CHECK_CHILD_ITEM_ENTER_TYPE, EnterTypeEnum.HISTORY);
        bundle.putSerializable(KeyConstants.KEY_SAFE_PATROL_CHECK_ITEM_DETAIL_RESULT, data);
        bundle.putString(KeyConstants.KEY_SAFE_PATROL_ITEM_ID, historyItemVo.getItem_id());
        UIHelper.showActivity(this, bundle, SafePatrolCheckItemDetailActivity.class);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(HttpCallbackEvent event) {

        if (KeyConstants.URL_GET_SAFE_PATROL_CHILD_ITEM_HISTORY_LIST.equals(event.getTag())) {       //获取检查项子项信息

            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
                mHUDUtil.show();
            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow()) {
                mHUDUtil.hidden();

                if (Constants.Result.SUCCESS == event.getEventResult()) {

                    SafePatrolChildItemHistoryVo childItemHistoryVo = (SafePatrolChildItemHistoryVo) (event.getObj());

                    if (mAdapter != null) {
                        mDataList.clear();
                        mAdapter.notifyDataSetChanged();
                        mAdapter.addItems(childItemHistoryVo.getDetails());
                    }

                } else if (Constants.Result.ERROR == event.getEventResult()) {
                    mHUDUtil.showErrorTip(event.getMsg(), new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {
                            callFinish();
                        }
                    });
                }
            }

        }

    }

    /**
     * 获取检查项子项信息
     */
    private void callHistoryChildItem() {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("check_id", historyItemVo.getCheck_id());

        SafePatrolServices.init().getSafePatrolChildItemHistoryList(params);
    }

}
